package edu.nju.mutest.combination;

import com.github.javaparser.ast.CompilationUnit;
import edu.nju.mutest.mutator.Mutator;
import edu.nju.mutest.mutator.factory.MutatorFactory;
import edu.nju.mutest.util.CompilationUnitUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * if there are two mutators. mutator A generates x1 mutants from source file, mutator B generate x2 mutants from source
 * file. After combination use AddStrategy, there are ABOUT x1 + x2 + x1 * x2 mutants. There are x1 mutants from A,
 * x2 mutants * from B, and every mutant from A can mutate into x2 mutants.
 * NOTE: if mutator A and B interact with each other, there may be more mutants.
 */
public class CartesianStrategy implements MutatorCombinationStrategy{

    @Override
    public void generateMutants(List<CompilationUnit> mutantList, CompilationUnit source, MutatorFactory mf) {
        // Continue to mutate based on existing variants
        List<CompilationUnit> newMutantList = new ArrayList<>();
        for (CompilationUnit cu : mutantList) {
            newMutantList.addAll(mf.newInstance(cu).mutate());
        }
        mutantList.addAll(newMutantList);

        // mutate based on source
        Mutator mutator = mf.newInstance(CompilationUnitUtil.clone(source));
        mutantList.addAll(mutator.mutate());
    }
}
